﻿using AutoMapper;
using MediatR;
using ProductionManagement.Application.Command.MaterialCommand;
using ProductionManagement.Domain.Dtos;
using ProductionManagement.Dtos;
using ProductionManagement.Infrstructre.lmp;
using ProductionManagement.Infrstructre.lnterface;
using SqlSugar;

namespace ProductionManagement.Application.CommandHandler.MaterialCommandHandlers
{
    public class MaterialShowCommandHandlers : IRequestHandler<MaterialShowCommand, PageDTO<MaterialBlikDto>>
    {
        private readonly IMaterialRepository _materialRepository;
        private readonly IItemSubpiecesRepository _itemSubpiecesRepository;
        private readonly ISqlSugarClient _db;
        private readonly IMapper _mapper;

        public MaterialShowCommandHandlers(IMaterialRepository materialRepository,
                                           IItemSubpiecesRepository itemSubpiecesRepository,
                                           ISqlSugarClient db,
                                           IMapper mapper)
        {
            _materialRepository = materialRepository;
            _itemSubpiecesRepository = itemSubpiecesRepository;
            _db = db;
            _mapper = mapper;
        }
        public async Task<PageDTO<MaterialBlikDto>> Handle(MaterialShowCommand request, CancellationToken cancellationToken)
        {
        //    var lists = (await _materialRepository.GetAll()).Where(t => t.Office_Id == request.Office_Id && t.IsDel == false).ToList();
            var list = (from a in await _materialRepository.GetAll() select a).Where(t => t.Office_Id == request.Office_Id && t.IsDel == false).ToList();
            var item = (from b in await _itemSubpiecesRepository.GetAll() select b).ToList();
            var lists = (from c in list
                          select new MaterialBlikDto
                          {

                              MaterialId = c.MaterialId,
                              MaterialNode = c.MaterialNode,
                              MSubject = c.MSubject,
                              MNumber = c.MNumber,
                              MWatchmakers = c.MWatchmakers,
                              MRemark = c.MRemark,
                              ProductId = c.ProductId,
                              PName = c.MPName,
                              PNumber = c.MPNumber,
                              MPSpec = c.MPSpec,
                              MPUnit = c.MPUnit,
                              MQuantity = c.MQuantity,
                              MProductPersonas = c.MProductPersonas,
                              MProcess = c.MProcess,
                              MWearRate = c.MWearRate,
                              AddName = c.AddName,
                              AddTime = c.AddTime,
                              Office_Id = c.Office_Id,
                              User_Id = c.User_Id,
                              ItemSubpiecesList = (from d in item
                                                   where d.MaterialNode == c.MaterialNode
                                                   select new ItemSubpiece
                                                   {
                                                       ItemSubpieceId = d.ItemId,
                                                       ProductId = d.ProductId,
                                                       IProductPersonas = d.IProductPersonas,
                                                       INumber = d.INumber,
                                                       PNumber = d.PNumber,
                                                       PName = d.PName,
                                                       PSpec = d.PSpec,
                                                       PUnit = d.PUnit,
                                                       ProductRatio = d.ProductRatio,
                                                       IProcess = d.IProcess,
                                                       IWearRate = d.IWearRate,
                                                       IPickingMethod = d.IPickingMethod,
                                                       MaterialNode = d.MaterialNode,
                                                       IMRemark = d.IMRemark,
                                                       AddName = d.AddName,
                                                       AddTime = d.AddTime,
                                                       Office_Id = d.Office_Id,
                                                       User_Id = d.User_Id
                                                   }).ToList()




                          });



            if (request.MSubject!=null)
            {
                lists= lists.Where(t => request.MSubject.Contains(t.MSubject)).ToList();
            }
            if (request.MaterialNode!= 0)
            {
                lists = lists.Where(t => t.MaterialNode == request.MaterialNode).ToList();
            }
            if(request.AddName!=null)
            {
                lists = lists.Where(t => request.AddName.Contains(t.AddName)).ToList();
            }
            if (request.MPName != null)
            {
                lists = lists.Where(t => request.MPName.Contains(t.PName)).ToList();
            }
            if (request.STime != null||request.LTime!=null)
            {
                lists = lists.Where(t =>t.AddTime>=request.STime && t.AddTime<=request.LTime).ToList();
            }
            PageDTO<MaterialBlikDto> page = new PageDTO<MaterialBlikDto>();
            // 执行查询并获取总数  
            page.Totalcount = lists.Count();
            page.Pagecount = (int)Math.Ceiling(page.Totalcount * 1.0 / request.pageSize);
            page.list = lists.OrderByDescending(t => t.MaterialId).Select(t => _mapper.Map<MaterialBlikDto>(t)).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
            return page;

        }
    }
}
